/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LineChart.java
 *
 * Created on Sep 25, 2011, 8:07:33 PM
 */
package InterNalFrame;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Dimension;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.data.xy.DefaultHighLowDataset;

/**
 *
 * @author Anatomy
 */
public final class candlestickChart extends javax.swing.JInternalFrame {
  

 JLabel         LB,SymbolName;
 JTextField     Symbol,Interval;
 JComboBox JB;
    /** Creates new form LineChart */
    public candlestickChart() {
        initComponents();
        
    }
 public candlestickChart(Dimension size,JTextField Symbol,JTextField Interval,JComboBox JB) throws SQLException {
     this.Symbol=Symbol;
     this.Interval=Interval;
       this.JB=JB;
      // t..hrow new UnsupportedOperationException("Not yet implemented");
          double height = size.getHeight();
        double width = size.getWidth()+12;
            
          Dimension xy=new Dimension();
          xy.setSize(width, height+10);
           Dimension setroot=new Dimension();
          setroot.setSize(width, height+5);
       this.setSize(xy);
       this.setLocation(-5,-5);
       javax.swing.plaf.InternalFrameUI ifu= this.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI)ifu).setNorthPane(null);

        final DefaultHighLowDataset dataset = createDataset();
        
  final JFreeChart chart = createChart(dataset);

           ValueAxis domainAxis =  chart.getXYPlot().getDomainAxis();
        domainAxis.setLowerMargin(0.0);
        domainAxis.setUpperMargin(0.0);
  final ChartPanel chartPanel = new ChartPanel(chart);
 // chartPanel.setPreferredSize(new java.awt.Dimension(600, 350));
  setContentPane(chartPanel);

    }
 private JFreeChart createChart(final DefaultHighLowDataset dataset) {
  final JFreeChart chart = ChartFactory.createCandlestickChart(
  null, null, null, dataset, false);

  return chart;
  }
 
  public DefaultHighLowDataset createDataset() throws SQLException {

    DefaultHighLowDataset data = null ;
      
 
     
      
       Connection connection = null;
        try {
        // Load the JDBC driver
        String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
        Class.forName(driverName);

        String serverName = "localhost";
        String mydatabase = "stock_data_base";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
        // System.out.println("DataBase Connected");
        String username = "root";
        String password = "root";
        connection = (Connection) DriverManager.getConnection(url, username, password);
       // System.out.println("DataBase Connected");

        Statement select = (Statement) connection.createStatement();
         ResultSet result1 = null;
       // javax.swing.JOptionPane.showMessageDialog(null, STOCK.txtSymbol.getText());
 if(JB.getSelectedIndex()==0){
      result1 = select.executeQuery("SELECT count(*) from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' YEAR) <= Date_Base");
 }else if(JB.getSelectedIndex()==1){
      result1 = select.executeQuery("SELECT count(*) from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' MONTH) <= Date_Base");
 }else if(JB.getSelectedIndex()==2){
      result1 = select.executeQuery("SELECT count(*) from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' DAY) <= Date_Base");   
 } 
 result1.next();
         int row=result1.getInt(1);
      //   javax.swing.JOptionPane.showMessageDialog(null, row);
   ResultSet result = null;
       // javax.swing.JOptionPane.showMessageDialog(null, STOCK.txtSymbol.getText());
 if(JB.getSelectedIndex()==0){
      result = select.executeQuery("SELECT * from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' YEAR) <= Date_Base");
 }else if(JB.getSelectedIndex()==1){
      result = select.executeQuery("SELECT * from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' MONTH) <= Date_Base");
 }else if(JB.getSelectedIndex()==2){
      result = select.executeQuery("SELECT * from stock_base_"+Symbol.getText().toLowerCase()+"  where Ticker_Base='"+Symbol.getText().toUpperCase()+"' AND DATE_SUB(CURDATE(),INTERVAL '"+Interval.getText()+"' DAY) <= Date_Base");   
 } 
 
       int serice = row;

  Date[] date = new Date[serice];
  double[] high = new double[serice];
  double[] low = new double[serice];
  double[] open = new double[serice];
  double[] close = new double[serice];
  double[] volume = new double[serice];
  int count=0;
        while (result.next()&& count<=serice) {
                date[count] = result.getDate(3);
                open[count] = result.getInt(5);
                high[count] = result.getInt(6);
                 low[count]= result.getInt(7);
                 close[count]= result.getInt(8);
                 volume[count]= result.getDouble(9);
            //     System.out.println(date[count]+"->"+count);
              count++;
                  
        }

	


      
      
   

 
  data = new DefaultHighLowDataset("", date, high, low, open, close, volume);
 
          }
        catch (ClassNotFoundException e) {
                System.out.println("DataBase Not Found");
        }
       

        return data;

    }

    
  


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(224, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(136, 136, 136))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(269, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

  

}
